Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

dot-json

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dot-json

Easily edit a json file from the CLI or NodeJS

  • 1.3.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

dot-json

huntr


Easily edit a json file from the CLI or NodeJS.

Install global

npm install -g dot-json

or local

npm install dot-json

Use from the CLI

dot-json myfile.json user.name "John Doe"
dot-json myfile.json user.email "john@example.com"
dot-json myfile.json foo..bar baz
dot-json myfile.json address '{"city":"Atlantis"}' --json-value

myfile.json now looks like

{
    "user": {
        "name": "John Doe",
        "email": "john@example.com"
    },
    "foo.bar": "baz",
    "address": {
        "city": "Atlantis"
    }
}
dot-json myfile.json user.name
John Doe
Usage:
  dot-json <file> <key-path>             Get a value from a json file by key-path
  dot-json <file> <key-path> <value>     Assign a value at a key-path
  dot-json <file> <key-path> --delete    Delete a key by key-path

Options:
  --indent=<n>      Indent with <n> of white space characters [default: auto] [--json-value]
  -d --delete       Delete the key-path
  -j --json-value   Parse the input value as a JSON string (to set whole objects or arrays)
  -h --help         Show this message with options
  -v --version      Print the version number

Quick tip for editing package.json

Add to .bash_profile:

alias package="dot-json package.json"

Use it like this:

package name "my-package"

Use it in NodeJS

Initialization
var DotJson = require('dot-json');
var myfile = new DotJson('myfile.json');
Writing

asynchronous

myfile.set('user.name', 'John Doe').set('user.email', 'john@example.com').save(function(){
  console.log('saved');
});

synchronous

myfile.set('user.name', 'John Doe').set('user.email', 'john@example.com').save();

myfile.json now looks like

{
    "user": {
        "name": "John Doe",
        "email": "john@example.com"
    }
}
Reading

asynchronous

myfile.get('user.name', function(value){
  // value = 'John Doe'
  console.log(value);
});

synchronous

var value = myfile.get('user.name');
// value = 'John Doe'
console.log(value);
Deleting

asynchronous

myfile.delete('user.name').save(function(){
  console.log('saved');
});

synchronous

myfile.delete('user.name').save();

myfile.json now looks like

{
    "user": {
        "email": "john@example.com"
    }
}

npmjs.org/package/dot-json

Keywords

FAQs

Package last updated on 02 May 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc